package com.sservice.web.controller.wx;

import com.sservice.banfu.vo.SalePersonVo;
import com.sservice.common.Constants;
import com.sservice.model.vo.OrderVo;
import com.sservice.service.service.IClienteleService;
import com.sservice.service.service.IGoodsService;
import com.sservice.service.service.IOfferService;
import com.sservice.service.service.IOrderService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.model.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/wx/order")
public class OrderWxController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IOrderService depService;
    @Autowired
    private IOfferService offerService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IClienteleService clienteleService;

    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> get(Order arg, HttpSession session) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");

            arg.setNo(Constants.createOrderNO());
            SalePersonVo sale = (SalePersonVo) session.getAttribute("current");
//            sale
            arg.setSaleId(String.valueOf(sale.getId()));
            arg.setSaleName(sale.getName());
//
//            clientele
            Clientele clientele = clienteleService.selectByPrimaryKey(
                    Integer.valueOf(arg.getClienteleId()));
            arg.setClienteleName(clientele.getComName());
            arg.setClienteleUserName(clientele.getPerson());
            arg.setClienteleUserPhone(clientele.getPhone());
            //goods
            Goods goods = goodsService.selectByPrimaryKey(Integer.valueOf(arg.getGoodsId()));
            arg.setGoodsName(goods.getName());
//            offer
            Offer offer = offerService.selectByPrimaryKey(Integer.valueOf(arg.getOfferId()));
            arg.setPrice(BigDecimal.valueOf(offer.getPrice()));
            arg.setTotal(BigDecimal.valueOf(offer.getPrice()).multiply(arg.getNumber()));

            Integer service_res = depService.insertSelective(arg);
            BaseResult res = new BaseResult<Integer>(true, service_res);
            return res;
        } catch (Exception e) {
            logger.error(e.getStackTrace().toString());
            return new BaseResult(false, "服务端错误!", 500);
        }

    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<OrderVo> query(OrderVo arg) {
        List<OrderVo> list = depService.queryByPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<OrderVo> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    /***
     * 我的客户订单
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryMyOrder", method = RequestMethod.GET)
    public ListResult<Object> queryMyOrder(OrderVo arg, HttpSession session) {

        SalePersonVo sale = (SalePersonVo) session.getAttribute("current");
        ListResult<Object> res;
        if (sale != null) {
            arg.setSaleId(String.valueOf(sale.getId()));
        } else {
            res = new ListResult(false, "请登录!", 401);
            return res;
        }
        List<OrderVo> list = depService.queryMyOrdersByPage(arg);
        int total = depService.queryMyOrderCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    /**
     * 我的销售员订单
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryMySaleOrder", method = RequestMethod.GET)
    public ListResult<Object> queryMySaleOrder(OrderVo arg, HttpSession session) {
        SalePersonVo sale = (SalePersonVo) session.getAttribute("current");
        ListResult<Object> res;
        if (sale != null) {
            arg.setSaleId(String.valueOf(sale.getId()));
        } else {
            res = new ListResult(false, "请登录!", 401);
            return res;
        }
        List<OrderVo> list = depService.queryMySalePersonOrdersByPage(arg);
        int total = depService.queryMySaleOrderCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
